In general, there are two programming models of parsers for working with XML infosets: document streaming (sometimes referred to as stream or stream-based parsing) and document object model (DOM). The DOM model involves creating in-memory objects representing the entire document tree and the complete infoset state for an XML document. In the DOM model, the entire XML document is stored in memory before processing. Once in memory, DOM trees can be navigated freely, traversed in any direction, and parsed arbitrarily. Moreover, comments are preserved. Thus, the DOM model provides maximum flexibility for developers. Also, nodes can be inserted or deleted in the DOM model. However, the cost of this flexibility is a potentially large memory footprint and significant processor requirements. This is due to need to hold the entire representation of the document in memory as objects for the duration of the document processing. In the DOM model, memory and processor requirements escalate quickly with increasing document size.
Document streaming refers to a program model in which XML infosets are transmitted and parsed serially at application runtime, often in real time, and often from dynamic sources whose contents are not precisely known beforehand. Stream-based parsers can start generating output immediately, and infoset elements can be discarded and garbage collected immediately after they are used. This allows stream-based parsers to have a smaller memory footprint and reduced processor requirements as compared to DOM model parsers. However, with stream-based parsers, a developer can only see the infoset state at one location in the document at a time. The developer is limited to what is sometimes referred to as the “cardboard tube” view of a document, referring to the view being limited to a portion of the document. The implication is that the developer needs to know what processing he/she wants to do before reading the XML document. Moreover, comments are not preserved and the document can only be traversed from top to bottom.
Many software developers are very comfortable with DOM Application Program Interface (API), and prefer the ability to navigate in different directions as their applications process the DOM tree. Also, in a DOM-based parsing, a developer can use multiple XPath expressions to extract data from the DOM without parsing the entire document each time. However, as documents get large, creating the entire DOM tree for a given document becomes unwieldy due to memory and processor requirements. Moreover, many applications may only be interested in specific segments of the document, rather than the document as a whole. The DOM model, however, requires the entire tree to be parsed and stored in memory, resulting in memory and processing time being expended to parse unwanted portions of the document.
In stream based parsing, the parsing model may be pull parsing, in which an application calls methods on an XML parsing library when it needs to interact with an XML infoset—that is, the client only gets (pulls) XML data when it explicitly asks for it. Alternatively, the parsing model may be push parsing, in which an XML parser sends (pushes) XML data to the application as the parser encounters elements in an XML infoset—that is, the parser sends the data whether or not the application is ready to use it at that time.